<template>
	<a-modal
		v-model:visible="visible"
		title="异常处理"
		:mask-closable="false"
		:width="600"
		:destroy-on-close="true"
		@ok="handleOk"
		@cancel="handleCancel"
	>
		<a-form ref="formRef" :model="formData">
			<a-form-item label="事件描述：" name="eventDesc">
				{{formData.eventDesc}}
			</a-form-item>
			<a-form-item label="处理描述：" name="procDesc" :rules="[{ required: true, message: '请输入处理描述' }]">
				<a-textarea v-model:value="formData.procDesc" placeholder="请输入处理描述" allow-clear :disabled="procStatus===2" />
			</a-form-item>
			<a-form-item label="关闭异常：" name="close" v-if="procStatus!=2">
				<a-switch v-model:checked="formData.close" checked-children="是" un-checked-children="否" />
			</a-form-item>
		</a-form>
	</a-modal>
</template>
<script setup name="bizEventExcHandle">
	import {cloneDeep} from "lodash-es";
	import bizEventApi from "@/api/biz/bizEventApi";
	import {message} from "ant-design-vue";

	let visible = $ref(false)
	const emit = defineEmits({successful: null})
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	//
	const procStatus = ref(0)

	// 打开弹框
	const onOpen = (id) => {
		visible = true
		bizEventApi.bizGoodsEventDetail({
			eventId: id
		}).then((data) => {
			let recordData = cloneDeep(data)
			procStatus.value = data.procStatus
			formData.value = Object.assign({}, recordData)
			formData.value.close = false
		})
	}

	//确定
	const handleOk = () => {
		if (procStatus.value != 2) {
			console.warn("formData=>",formData.value)
			formRef.value
				.validate()
				.then(() => {
					bizEventApi.bizGoodsEventExcHandle({
						id: formData.value.id,
						procDesc: formData.value.procDesc,
						close: formData.value.close
					}).then(() => {
						message.success("异常处理成功")
						handleClose()
						emit('successful')
					})
				})
		} else {
			handleClose()
		}
	}

	//关闭
	const handleClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible = false
	}

	defineExpose({
		onOpen
	})
</script>
